import re
def validate_email(email):
    pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    return bool(re.fullmatch(pattern, email))

def classify_email(email):

    if not validate_email(email):
        return "无效邮箱"

    domain = email.split('@')[1]
    if '.edu' in domain or '.ac.' in domain:
        return "教育机构"
    elif any(x in domain for x in ['.com', '.org', '.net']):
        return "公司"
    else:
        return "个人"


def extract_email_parts(email):
    if not validate_email(email):
        return None
    username, domain = email.split('@')
    tld = domain.split('.')[-1]
    return {
        'username': username,
        'domain': domain,
        'tld': tld
    }


def obfuscate_email(email):
    if not validate_email(email):
        return email
    username, domain = email.split('@')
    if len(username) > 1:
        obscured_username = username[0] + '***' + username[-1]
    else:
        obscured_username = username + '***'
    domain_parts = domain.split('.')
    if len(domain_parts) > 1:
        obscured_domain = '**' + '.'.join(domain_parts[:-1]) + '.' + domain_parts[-1]
    else:
        obscured_domain = '**' + domain

    return f"{obscured_username}@{obscured_domain}"

test_emails = [
    "user.name@university.ac.edu",
    "john.doe+test@example.com",
    "invalid.email@.com",
    "another_user@sub.domain.org",
    "private@gmail.com",
    "a@b.co",
    "test@test.edu.cn"
]

for email in test_emails:
    print(f"\n测试邮箱: {email}")
    print(f"验证结果: {'有效' if validate_email(email) else '无效'}")
    if validate_email(email):
        print(f"分类结果: {classify_email(email)}")
        print(f"提取信息: {extract_email_parts(email)}")
        print(f"脱敏处理: {obfuscate_email(email)}")